<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>layui</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <link rel="stylesheet" href="/lib/layui-v2.6.3/css/layui.css" media="all">
  <link rel="stylesheet" href="/css/public.css" media="all">
  <style>
    body {
      background-color: #ffffff;
    }
  </style>
</head>
<body>
<br />
<br />
<form class="layui-form layuimini-form layui-form-pane">
  <div class="layui-form-item layui-col-sm5" pane>
    <label class="layui-form-label">添加管理员</label>
    <div class="layui-input-block">
      <input type="checkbox"  id="adduser",name="adduser" value=0  lay-skin="switch" lay-filter="addu" lay-text="是|否">
    </div>
  </div>
  <div class="layui-form-item layui-col-sm5" pane>
    <label class="layui-form-label">删除管理员</label>
    <div class="layui-input-block">
      <input type="checkbox" id="deleteuser" name="deleteuser" value=0  lay-skin="switch" lay-filter="deleteu" lay-text="是|否">
    </div>
  </div>
  <div class="layui-form-item layui-col-sm5" pane>
    <label class="layui-form-label">修改苗药信息</label>
    <div class="layui-input-block">
      <input type="checkbox" id="updatemiaosample" name="updatemiaosample" value=0  lay-skin="switch" lay-filter="updatem" lay-text="是|否">
    </div>
  </div>
  <div class="layui-form-item layui-col-sm5" pane>
    <label class="layui-form-label">删除苗药信息</label>
    <div class="layui-input-block">
      <input type="checkbox" id="deletemiaosample" name="deletemiaosample" value=0 lay-skin="switch" lay-filter="deletem" lay-text="是|否">
    </div>
  </div>
  <div class="layui-form-item layui-col-sm5" pane>
    <label class="layui-form-label">添加苗药信息</label>
    <div class="layui-input-block">
      <input type="checkbox" id="addmiaosample" name="addmiaosample" value=0 lay-skin="switch" lay-filter="addm" lay-text="是|否">
    </div>
  </div>
  <div class="layui-form-item layui-col-sm5" pane>
    <label class="layui-form-label">修改管理员权限</label>
    <div class="layui-input-block">
      <input type="checkbox" id="updatepermission" name="updatepermission" value=0 lay-skin="switch" lay-filter="upa" lay-text="是|否">
    </div>
  </div>
  <div class="layui-form-item">
    <div class="layui-input-block">
      <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
    </div>
  </div>
</form>
<script src="/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script>
  layui.use(['form'], function () {
    var form = layui.form,
            layer = layui.layer,
            $ = layui.$;
    //数据显示
    var session=window.sessionStorage;
    console.log(session.getItem("adduser"));
    if(session.getItem("adduser")=="1"){
      $("#adduser").attr("checked","checked")
      $("#adduser").val(1)
    }
    if(session.getItem("deleteuser")=="1"){
      $("#deleteuser").attr("checked","checked")
      $("#deleteuser").val(1)
    }
    if(session.getItem("addmiaosample")=="1"){
      $("#addmiaosample").attr("checked","checked")
      $("#addmiaosample").val(1)
    }
    if(session.getItem("updatemiaosample")=="1"){
      $("#updatemiaosample").attr("checked","checked")
      $("#updatemiaosample").val(1)
    }
    if(session.getItem("deletemiaosample")=="1"){
      $("#deletemiaosample").attr("checked","checked")
      $("#deletemiaosample").val(1)
    }
    if(session.getItem("updatepermission")=="1"){
      $("#updatepermission").attr("checked","checked")
      $("#updatepermission").val(1)
    }
    form.render('checkbox')
    //监听状态改变与否
    form.on('switch(addu)', function (data) {
      if(data.elem.checked==true){ //checked
        $("#adduser").val(1);
      }else {
        $("#adduser").val(0)
      }
    });
    form.on('switch(deleteu)', function (data) {
      if(data.elem.checked==true){ //checked
        $("#deleteuser").val(1);
      }else {
        $("#deleteuser").val(0)
      }
    });
    form.on('switch(updatem)', function (data) {
      if(data.elem.checked==true){ //checked
        $("#updatemiaosample").val(1);
      }else {
        $("#updatemiaosample").val(0)
      }
    });
    form.on('switch(deletem)',function(data){
      if(data.elem.checked==true){
        $("#deletemiaosample").val(1)
      }else $("#deletemiaosample").val(0)
    })
    form.on('switch(addm)',function(data){
      if(data.elem.checked==true)
        $("#addmiaosample").val(1)
      else $("#addmiaosample").val(0)
    })
    form.on('switch(upa)',function(data){
      if(data.elem.checked==true)
        $("#updatepermission").val(1)
      else
        $("#updatepermission").val(0)
    });
    // 监听搜索操作
    form.on('submit(data-search-btn)', function (data) {
      console.log(data)
      var result = JSON.stringify(data.field);
      layer.alert(result, {
        title: '最终的搜索信息'
      });
      // //执行搜索重载
      // table.reload('currentTableId', {
      //     page: {
      //         curr: 1
      //     }
      //     , where: {
      //         searchParams: result
      //     }
      // }, 'data');

      return false;
    });
    //监听提交
    form.on('submit(saveBtn)', function (data) {
      // var index = layer.alert(JSON.stringify(data.field), {
      //   title: '最终的提交信息'
      // }, function () {
      //
      //   // 关闭弹出层
      //   layer.close(index);
      //   var iframeIndex = parent.layer.getFrameIndex(window.name);
      //   parent.layer.close(iframeIndex);
      //
      // });
      var jsonData=JSON.stringify(data.field)
      console.log("显示的数据："+jsonData)
      $.post(
              "/updatePermission",
              {
                'adduser':$("#adduser").val(),
                'deleteuser':$("#deleteuser").val(),
                'updatemiaosample':$("#updatemiaosample").val(),
                'deletemiaosample':$("#deletemiaosample").val(),
                'addmiaosample':$("#addmiaosample").val(),
                'querymiaosample':1,
                'updatepermission':$("#updatepermission").val(),
                'uid':session.getItem("uid")
              },//发送的数据
              function (data){
                if(data){
                  layer.msg("修改成功",{
                    icon: 1,
                    time: 500
                  },
                  function (){
                    var index = parent.layer.getFrameIndex(window.name);
                    parent.layer.close(index);
                    parent.location.reload();
                  })
                }else {
                  layer.msg("修改失败",{
                            icon: 2,
                            time: 500
                          },
                          function (){
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(index);
                            parent.location.reload();
                          })
                }
              }
      )
      form.render('checkbox')
      return false;
    });
  });
</script>
</body>
</html>